package cn.circle.system.dao.impl;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import cn.circle.commom.dao.impl.BaseDaoImpl;
import cn.circle.system.dao.ICommodityCategoryDao;
import cn.circle.system.model.CommodityCategory;

/**
 * 具体实现CommodityCategory的CRUD 作者：余兆康 日期：2016/9/18
 * 
 */
@Repository
public class CommodityCategoryDaoImpl extends BaseDaoImpl<CommodityCategory> implements ICommodityCategoryDao {

	@Override
	public boolean deleteAll(String ids) {
		String hql = "delete CommodityCategory s where s.categoryId in (" + ids + ") ";
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql);
		// query.setString(0, ids);
		return (query.executeUpdate() > 0);
	}

	@Override
	public boolean removeAll(String ids) {
		String hql = "update CommodityCategory s set s.status = 0 where s.categoryId in (" + ids + ") ";
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql);
		return (query.executeUpdate() > 0);
	}

	@Override
	public boolean updateAll(CommodityCategory c) {
		StringBuffer hql = new StringBuffer("update CommodityCategory s set ");

		// 拼接修改条件
		if (c.getName() != null && !"".equals(c.getName())) {
			hql.append("  s.name = '" + c.getName() + "' ");
		}
		if (c.getSummary() != null ) {
			hql.append(" , s.summary = '" + c.getSummary() + "' ");
		}
		if (c.getStatus() != null && !"".equals(c.getStatus())) {
			hql.append(" , s.status = '" + c.getStatus() + "' ");
		}
		hql.append(" where s.categoryId =" + c.getCategoryId() + " ");
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql.toString());
		return (query.executeUpdate() > 0);
	}
}
